package com.jinmdz.fmis.api.rec.service;

import com.jinmdz.fmis.api.api.model.common.IdData;
import com.jinmdz.fmis.api.api.model.common.IdVersionData;
import com.jinmdz.fmis.api.api.service.DictionaryService;
import com.jinmdz.fmis.api.api.service.UserService;
import com.jinmdz.fmis.api.base.BaseService;
import com.jinmdz.fmis.api.constant.MessageConst;
import com.jinmdz.fmis.api.model.config.ConfigApp;
import com.jinmdz.fmis.api.model.config.ConfigData;
import com.jinmdz.fmis.api.model.config.dictionary.TaskStateCode;
import com.jinmdz.fmis.api.model.system.UserItem;
import com.jinmdz.fmis.api.rec.model.recdispatchcar.RecDispatchCarData;
import com.jinmdz.fmis.api.rec.model.recinfo.*;
import com.jinmdz.fmis.api.wrapper.RecInfoWrapper;
import com.jinmdz.fmis.common.ext.ArrayListExt;
import com.jinmdz.fmis.common.util.BeanUtil;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.common.util.DateUtil;
import com.jinmdz.fmis.core.base.BaseResult;
import com.jinmdz.fmis.core.constant.EBusinessType;
import com.jinmdz.fmis.core.constant.EDictCode;
import com.jinmdz.fmis.core.exception.ActionException;
import com.jinmdz.fmis.core.model.SearchLoadData;
import com.jinmdz.fmis.dao.dao.*;
import com.jinmdz.fmis.dao.model.dispatch.DispatchCarItem;
import com.jinmdz.fmis.dao.model.dispatchcar.DispatchCar;
import com.jinmdz.fmis.dao.model.recactualitem.RecActualItem;
import com.jinmdz.fmis.dao.model.recgpsinfo.RecGpsInfo;
import com.jinmdz.fmis.dao.model.recgpsinfo.RecGpsInfoData;
import com.jinmdz.fmis.dao.model.recinfo.RecInfoAllWxData;
import com.jinmdz.fmis.dao.model.recinfo.RecInfoSearh;
import com.jinmdz.fmis.dao.model.recinfo.RecInfoWx;
import com.jinmdz.fmis.dao.model.recselitem.RecSelItem;
import com.jinmdz.fmis.dao.model.service.ServiceItem;
import com.jinmdz.fmis.mapper.entity.*;
import com.jinmdz.fmis.mapper.mapper.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

/**
 * @author zhanglr
 * @date 2020/6/10 11:23
 */
@Service
public class RecInfoService extends BaseService {
    @Resource
    private ConfigApp configApp;
    @Resource
    private ConfigData configData;
    @Resource
    private RecInfoMapper recInfoMapper;
    @Resource
    private ServiceItemDao serviceItemDao;
    @Resource
    private RecInfoDao recInfoDao;
    @Resource
    private UserService userService;
    @Resource
    private DictionaryService dictionaryService;
    @Resource
    private RecDispatchCarMapper recDispatchCarMapper;
    @Resource
    private RecDispatchCarDao recDispatchCarDao;
    @Resource
    private RecSelItemDao recSelItemDao;
    @Resource
    private RecDriverMapper recDriverMapper;
    @Resource
    private RecPorterMapper recPorterMapper;
    @Resource
    private RecInfoWrapper recInfoWrapper;
    @Resource
    private RecInfoService recInfoService;
    @Resource
    private SystemUserDao systemUserDao;
    @Resource
    private TaskStateCode taskStateCode;
    @Resource
    private RecSelItemMapper recSelItemMapper;
    @Resource
    private RecActualItemMapper recActualItemMapper;

    /**
     * 保存车辆调度信息到dispatch_car表中
     *
     * @param
     * @return
     * @author ZhangLiRui
     * @date 2020/6/11 9:47
     */
    @Transactional(rollbackFor = Exception.class)
    public BaseResult saveRecInfo(UserItem userItem, RecInfoData data) throws ActionException {
        String recCode = data.getRecCode();
        //主键id
        Integer id = data.getId();
        // 业务编码不为空
        if (DataUtil.invalid(recCode)) {
            // 不存在接运编码时，认定为新增，生成新业务编码
            recCode = recInfoService.getRecInfoRecCodeAsNew();
            data.setRecCode(recCode);
            id = insertRectInfo(userItem, data);
        } else {
            updateRecInfo(userItem, data);
        }

        return loadRecInfoWithId(new IdData(id));
    }

    private Integer insertRectInfo(UserItem userItem, RecInfoData data) {
        RecInfoEntity recInfoEntity = getEntity(userItem, RecInfoEntity.class);
        String funeralParlourCode = recInfoEntity.getFuneralParlourCode();
        // 覆盖数据
        BeanUtil.copy2Bean(data, recInfoEntity);
        //复写回殡仪馆代码
        recInfoEntity.setFuneralParlourCode(funeralParlourCode);
        recInfoMapper.insertRecInfo(recInfoEntity);


        // 判断保存信息结果
        if (DataUtil.invalid(recInfoEntity.getId())) {
            throw exception("保存信息失败");
        }

        //保存车辆调度信息
        RecDispatchCarData dispatchCarReqData = data.getDispatchCar();
        //判断系统是否是设置调度信息必填，如果是则验证调度信息不为空
        if (configApp.getRecDispatchEnable() && DataUtil.isNull(dispatchCarReqData)) {
            throw exception("调度信息必填");
        }
        //判断接运信息
        if (!DataUtil.isNull(dispatchCarReqData)) {
            RecDispatchCarEntity recDispatchCarEntity = getEntity(userItem, RecDispatchCarEntity.class);
            String funeralParlourCodeValue = recDispatchCarEntity.getFuneralParlourCode();
            // 覆盖数据
            BeanUtil.copy2Bean(dispatchCarReqData, recDispatchCarEntity);
            //殡仪馆代码回写
            recDispatchCarEntity.setFuneralParlourCode(funeralParlourCodeValue);
            //接运信息
            recDispatchCarEntity.setRecId(recInfoEntity.getId());
            //当前操作用户为调度人员
            recDispatchCarEntity.setDispatchUserId(userItem.getId());
            //前操作用户为调度人员姓名
            recDispatchCarEntity.setDispatchUserName(userItem.getFullName());
            //保存车辆调度信息
            recDispatchCarMapper.insertRecDispatchCar(recDispatchCarEntity);

            // 判断保存信息结果
            if (DataUtil.invalid(recDispatchCarEntity.getId())) {
                throw exception("保存信息失败");
            }
        }

        //保存接运司机信息
        ArrayList<Integer> recInfoDriver = data.getRecInfoDriver();
        if (recInfoDriver != null && recInfoDriver.size() > 0) {
            for (Integer id : recInfoDriver) {
                RecDriverEntity entity = getEntity(userItem, RecDriverEntity.class)
                        .setRecId(recInfoEntity.getId())
                        .setSystemUserId(id);

                //插入接运司机数据
                recDriverMapper.insertRecDriver(entity);

                if (DataUtil.invalid(entity.getId())) {
                    throw exception("保存抬尸工信息失败");
                }

            }

        }

        //保存抬尸工信息
        ArrayList<Integer> recInfoPorter = data.getRecInfoPorter();

        if (recInfoPorter != null && recInfoPorter.size() > 0) {
            for (Integer id : recInfoPorter) {
                RecPorterEntity entity = getEntity(userItem, RecPorterEntity.class)
                        .setRecId(recInfoEntity.getId())
                        .setSystemUserId(id);
                //保存抬尸工数据
                recPorterMapper.insertRecPorter(entity);

                if (DataUtil.invalid(entity.getId())) {
                    throw exception("保存接运司机信息失败");
                }

            }

        }
        //实际物品
        ArrayList<RecActualItem> actualList = data.getRecActualItem();
        if (actualList != null && !actualList.isEmpty()) {
            for (RecActualItem actualItem : actualList) {
                RecActualItemEntity recInfoEntityAdd = getEntity(userItem, RecActualItemEntity.class);
                // 拷贝数据
                BeanUtil.copy2Bean(actualItem, recInfoEntityAdd);
                recInfoEntityAdd.setRecId(recInfoEntity.getId());
                recInfoEntityAdd.setRecCode(recInfoEntity.getRecCode());
                //补齐验证数据
                if (DataUtil.isNull(recInfoEntityAdd.getItemCharge())) {
                    recInfoEntityAdd.setItemCharge(new BigDecimal(0));
                }
                //优惠金额
                if (DataUtil.isNull(recInfoEntityAdd.getDiscountCharge())) {
                    recInfoEntityAdd.setDiscountCharge(new BigDecimal(0));
                }
                //惠民金额
                if (DataUtil.isNull(recInfoEntityAdd.getBenefitCharge())) {
                    recInfoEntityAdd.setBenefitCharge(new BigDecimal(0));
                }
                //实际收费金额
                if (DataUtil.isNull(recInfoEntityAdd.getRealityCharge())) {
                    recInfoEntityAdd.setRealityCharge(new BigDecimal(0));
                }
                //结算时间
                if (DataUtil.isNull(recInfoEntityAdd.getChargeTime())) {
                    recInfoEntityAdd.setChargeTime(new Date());
                }
                //是否不可结算
                if (DataUtil.isNull(recInfoEntityAdd.getAsUnsettled())) {
                    recInfoEntityAdd.setAsUnsettled(0);
                }
                //是否结算
                if (DataUtil.isNull(recInfoEntityAdd.getAsSettled())) {
                    recInfoEntityAdd.setAsSettled(0);
                }
                recActualItemMapper.insertRecActualItem(recInfoEntityAdd);
            }
        }
        //随车物品
        ArrayList<RecSelItem> selList = data.getRecSelItem();
        if (selList != null && !selList.isEmpty()) {
            for (RecSelItem selItem : selList) {
                RecSelItemEntity recSelItemEntityAdd = getEntity(userItem, RecSelItemEntity.class);
                // 拷贝数据
                BeanUtil.copy2Bean(selItem, recSelItemEntityAdd);
                recSelItemEntityAdd.setRecId(recInfoEntity.getId());
                //补齐验证数据
                if (DataUtil.isNull(recSelItemEntityAdd.getItemCharge())) {
                    recSelItemEntityAdd.setItemCharge(new BigDecimal(0));
                }
                //优惠金额
                if (DataUtil.isNull(recSelItemEntityAdd.getDiscountCharge())) {
                    recSelItemEntityAdd.setDiscountCharge(new BigDecimal(0));
                }
                //惠民金额
                if (DataUtil.isNull(recSelItemEntityAdd.getBenefitCharge())) {
                    recSelItemEntityAdd.setBenefitCharge(new BigDecimal(0));
                }
                //实际收费金额
                if (DataUtil.isNull(recSelItemEntityAdd.getRealityCharge())) {
                    recSelItemEntityAdd.setRealityCharge(new BigDecimal(0));
                }
                //结算时间
                if (DataUtil.isNull(recSelItemEntityAdd.getChargeTime())) {
                    recSelItemEntityAdd.setChargeTime(new Date());
                }
                //是否不可结算
                if (DataUtil.isNull(recSelItemEntityAdd.getAsUnsettled())) {
                    recSelItemEntityAdd.setAsUnsettled(0);
                }
                //是否结算
                if (DataUtil.isNull(recSelItemEntityAdd.getAsSettled())) {
                    recSelItemEntityAdd.setAsSettled(0);
                }
                recSelItemMapper.insertRecSelItem(recSelItemEntityAdd);
            }
        }

        return recInfoEntity.getId();
    }

    private void updateRecInfo(UserItem userItem, RecInfoData data) throws ActionException {
        // 验证主键和数据版本是否存在
        if (DataUtil.isNull(data.getId(), data.getVersion())) {
            throw exception("请求主键与版本号不能为空");
        }

        RecInfoEntity recInfoEntity = recInfoMapper.getRecInfoById(data.getId());
        if (DataUtil.isNull(recInfoEntity) || DataUtil.isNull(recInfoEntity.getId(), recInfoEntity.getVersion())) {
            throw exception("类型主键错误，不存在此类型信息");
        }

        // 验证信息版本
        if (!DataUtil.equals(data.getVersion(), recInfoEntity.getVersion())) {
            throw exception(versionError("物品服务类型信息"));
        }
        // 判断流程状态值
        if (!DataUtil.inIntegers(data.getTaskStateCode()
                , taskStateCode.getYuYueDengJi()
                , taskStateCode.getYiDiaoDu()
                , taskStateCode.getYiChuChe()
                , taskStateCode.getYiHuiChe()
                , taskStateCode.getFanKongChe())) {
            throw exception("接运信息状态值错误");
        }

        // 覆盖数据
        BeanUtil.copy2Bean(data, recInfoEntity);
        //判断系统是否是设置调度信息必填，如果是则验证调度信息不为空
        if (configApp.getRecDispatchEnable() && DataUtil.isNull(data.getDispatchCar())) {
            throw exception("调度信息必填");
        }
        //更新调度车辆信息
        updateDispatchCar(userItem, data, recInfoEntity);
        //更新抬尸工
        updateRecTaskPorter(userItem, data, recInfoEntity);
        //更新司机
        updateRecInfoDriver(userItem, data, recInfoEntity);
        //新增或更新随车物品
        recInfoWrapper.updateGoodsServices(userItem, recInfoEntity.getId(), data.getRecSelItem());

        // 删除物品服务
        ArrayList<RecSelItemEntity> deleteEntities = recInfoWrapper.deleteGoodsServices(userItem, recInfoEntity.getId(), data.getRelSelDelete());
        if (!deleteEntities.isEmpty()) {
            RecSelItemEntity deleteEntity = null;
            for (int m = 0; m < deleteEntities.size(); m++) {
                deleteEntity = deleteEntities.get(m);
                recSelItemMapper.deletedForRecSelItem(deleteEntity.getId(), userItem.getId(), deleteEntity.getVersion());
            }
        }
        //更新或新增实际使用物品
        recInfoWrapper.updateActualItem(userItem, recInfoEntity.getId(), data.getRecActualItem());
        // 删除物品服务
        ArrayList<RecActualItemEntity> deleteActualEntityList = recInfoWrapper.deleteActualItem(userItem, recInfoEntity.getId(), data.getRelSelDelete());
        if (!deleteActualEntityList.isEmpty()) {
            RecActualItemEntity deleteEntity = null;
            for (int m = 0; m < deleteEntities.size(); m++) {
                deleteEntity = deleteActualEntityList.get(m);
                recActualItemMapper.deletedForRecActualItem(deleteEntity.getId(), userItem.getId(), deleteEntity.getVersion());
            }
        }
        //修改数据
        recInfoMapper.updateRecInfo(recInfoEntity);


    }

    /**
     * 验接运数据
     *
     * @param userItem
     * @param data
     * @throws ActionException
     */
    private RecInfoEntity validataRecInfo(UserItem userItem, RecInfoData data) throws ActionException {
        if (DataUtil.isNull(data.getId(), data.getVersion())) {
            throw exception("请求主键与版本号不能为空");
        }

        RecInfoEntity recInfoEntity = recInfoMapper.getRecInfoById(data.getId());
        if (DataUtil.isNull(recInfoEntity) || DataUtil.isNull(recInfoEntity.getId(), recInfoEntity.getVersion())) {
            throw exception("类型主键错误，不存在此类型信息");
        }

        // 验证信息版本
        if (!DataUtil.equals(data.getVersion(), recInfoEntity.getVersion())) {
            throw exception(versionError("物品服务类型信息"));
        }
        // 判断流程状态值
        if (!DataUtil.inIntegers(data.getTaskStateCode()
                , taskStateCode.getYuYueDengJi()
                , taskStateCode.getYiDiaoDu()
                , taskStateCode.getYiChuChe()
                , taskStateCode.getYiHuiChe()
                , taskStateCode.getFanKongChe())) {
            throw exception("接运信息状态值错误");
        }
        return recInfoEntity;

    }

    /**
     * 更新接运信息
     *
     * @param userItem
     * @param data
     * @param recInfoEntity
     * @throws ActionException
     */
    private void updateRecInfo(UserItem userItem, RecInfoData data, RecInfoEntity recInfoEntity) throws ActionException {
        // 覆盖数据
        BeanUtil.copy2Bean(data, recInfoEntity);
        //更新调度车辆信息
        updateDispatchCar(userItem, data, recInfoEntity);
        //更新抬尸工
        updateRecTaskPorter(userItem, data, recInfoEntity);
        //更新司机
        updateRecInfoDriver(userItem, data, recInfoEntity);
        //新增或更新随车物品
        recInfoWrapper.updateGoodsServices(userItem, recInfoEntity.getId(), data.getRecSelItem());

        // 删除物品服务
        ArrayList<RecSelItemEntity> deleteEntities = recInfoWrapper.deleteGoodsServices(userItem, recInfoEntity.getId(), data.getRelSelDelete());
        if (!deleteEntities.isEmpty()) {
            RecSelItemEntity deleteEntity = null;
            for (int m = 0; m < deleteEntities.size(); m++) {
                deleteEntity = deleteEntities.get(m);
                recSelItemMapper.deletedForRecSelItem(deleteEntity.getId(), userItem.getId(), deleteEntity.getVersion());
            }
        }
        //更新或新增实际使用物品
        recInfoWrapper.updateActualItem(userItem, recInfoEntity.getId(), data.getRecActualItem());
        // 删除物品服务
        ArrayList<RecActualItemEntity> deleteActualEntityList = recInfoWrapper.deleteActualItem(userItem, recInfoEntity.getId(), data.getRelSelDelete());
        if (!deleteActualEntityList.isEmpty()) {
            RecActualItemEntity deleteEntity = null;
            for (int m = 0; m < deleteEntities.size(); m++) {
                deleteEntity = deleteActualEntityList.get(m);
                recActualItemMapper.deletedForRecActualItem(deleteEntity.getId(), userItem.getId(), deleteEntity.getVersion());
            }
        }
        //修改数据
        recInfoMapper.updateRecInfo(recInfoEntity);
    }

    /**
     * 更新接运信息车辆调度信息，没有调度新建调度信息。
     *
     * @param userItem
     * @param data
     * @param recInfoEntity
     * @throws ActionException
     */
    private void updateRecInfoDispatchCar(UserItem userItem, RecInfoData data, RecInfoEntity recInfoEntity) throws ActionException {
        // 覆盖数据
        BeanUtil.copy2Bean(data, recInfoEntity);
        //更新调度车辆信息
        if (!DataUtil.isNull(data.getDispatchCar())) {
            if (data.getDispatchCar().getId().intValue() == 0) {
                //保存调度车辆
                RecDispatchCarEntity recDispatchCarEntity = getEntity(userItem, RecDispatchCarEntity.class);
                // 覆盖数据
                BeanUtil.copy2Bean(data.getDispatchCar(), recDispatchCarEntity);
                recDispatchCarEntity.setRecId(data.getId());
                //新增调度信息
                recDispatchCarMapper.insertRecDispatchCar(recDispatchCarEntity);
            } else if (data.getDispatchCar().getId().intValue() >= 0) {
                //更新调度车辆
                updateDispatchCar(userItem, data, recInfoEntity);
            }
        }


        //更新抬尸工
        updateRecTaskPorter(userItem, data, recInfoEntity);
        //更新司机
        updateRecInfoDriver(userItem, data, recInfoEntity);
        //新增或更新随车物品
        recInfoWrapper.updateGoodsServices(userItem, recInfoEntity.getId(), data.getRecSelItem());

        // 删除物品服务
        ArrayList<RecSelItemEntity> deleteEntities = recInfoWrapper.deleteGoodsServices(userItem, recInfoEntity.getId(), data.getRelSelDelete());
        if (!deleteEntities.isEmpty()) {
            RecSelItemEntity deleteEntity = null;
            for (int m = 0; m < deleteEntities.size(); m++) {
                deleteEntity = deleteEntities.get(m);
                recSelItemMapper.deletedForRecSelItem(deleteEntity.getId(), userItem.getId(), deleteEntity.getVersion());
            }
        }
        //更新或新增实际使用物品
        recInfoWrapper.updateActualItem(userItem, recInfoEntity.getId(), data.getRecActualItem());
        // 删除物品服务
        ArrayList<RecActualItemEntity> deleteActualEntityList = recInfoWrapper.deleteActualItem(userItem, recInfoEntity.getId(), data.getRelSelDelete());
        if (!deleteActualEntityList.isEmpty()) {
            RecActualItemEntity deleteEntity = null;
            for (int m = 0; m < deleteEntities.size(); m++) {
                deleteEntity = deleteActualEntityList.get(m);
                recActualItemMapper.deletedForRecActualItem(deleteEntity.getId(), userItem.getId(), deleteEntity.getVersion());
            }
        }
        //修改数据
        recInfoMapper.updateRecInfo(recInfoEntity);
    }

    /**
     * 更新出车确认的状态
     *
     * @param userItem
     * @param data
     * @throws ActionException
     */
    private void confirmOutCar(UserItem userItem, RecInfoData data) throws ActionException {
        // 验证主键和数据版本是否存在
        RecInfoEntity recInfoEntity = validataRecInfo(userItem, data);
        if (!DataUtil.isNull(recInfoEntity)) {
            //已调度车辆，接运信息为已出车
            if (recInfoEntity.getTaskStateCode().intValue() == taskStateCode.getYiDiaoDu().intValue()) {
                //更新车辆接运信息状态为已出车状态
                recInfoEntity.setTaskStateCode(taskStateCode.getYiChuChe());
                recInfoEntity.setTaskState(taskStateCode.getYiChuChe_name());
                data.setTaskStateCode(taskStateCode.getYiChuChe());
                data.setTaskState(taskStateCode.getYiChuChe_name());
                //更新确认出车的状态
                updateRecInfo(userItem, data, recInfoEntity);
            } else {
                throw exception("请求非调度车辆不能确认出车");
            }
        }

    }

    /**
     * 更新接运信息车辆调度状态，预约登记为已调度状态
     *
     * @param userItem
     * @param data
     * @throws ActionException
     */
    public BaseResult confirmDispatchCar(UserItem userItem, RecInfoData data) throws ActionException {
        // 验证主键和数据版本是否存在
        RecInfoEntity recInfoEntity = validataRecInfo(userItem, data);
        if (!DataUtil.isNull(recInfoEntity)) {
            //已调度车辆，接运信息为预约登记状态
            if (recInfoEntity.getSourceCode().intValue() == taskStateCode.getYuYueDengJi().intValue() || recInfoEntity.getSourceCode().intValue() == taskStateCode.getYiDiaoDu().intValue()) {
                //更新车辆接运信息状态为已出车状态
                recInfoEntity.setTaskStateCode(taskStateCode.getYiDiaoDu());
                recInfoEntity.setTaskState(taskStateCode.getYiDiaoDu_name());
                //更新车辆调度的状态
                updateRecInfoDispatchCar(userItem, data, recInfoEntity);

            } else {
                throw exception("非预【约登记车辆】或【已调度车辆】不能更新调度状态");
            }
            return loadRecInfoWithId(new IdData(recInfoEntity.getId()));
        } else {
            //没有结果
            return successData(new RecGpsInfoData());
        }

    }

    /**
     * 更新接调度信息
     *
     * @param userItem 当前帐号
     * @param data     接运数据
     * @param entity   接运任务
     * @return
     * @author LiCongLu
     * @date 2020-02-13 15:30
     */
    private void updateDispatchCar(UserItem userItem, RecInfoData data, RecInfoEntity entity) throws
            ActionException {
        // 删除调度信息
        if (DataUtil.isNull(data.getDispatchCar())) {
            // 清除车辆调度信息
            recDispatchCarDao.deleteRecDispatchCarByRecId(entity.getId(), userItem.getId());
        } else {
            //验证接运
            if (data == null || entity == null || (data != null && entity != null && !data.getId().equals(entity.getId()))) {
                throw exception("接运主键与实际不符");
            }
            //获得调度车辆信息
            RecDispatchCarData dispatchCar = data.getDispatchCar();

            // 验证id和版本
            if (DataUtil.isNull(dispatchCar.getId(), dispatchCar.getVersion())) {
                throw exception("保存调度主键与版本号不能为空");
            }
            //获得数据库版本
            RecDispatchCarEntity dispatchCarEntityOld = recDispatchCarMapper.getRecDispatchCarById(dispatchCar.getId());
            //验证版本不符
            if (dispatchCarEntityOld != null && !dispatchCar.getVersion().equals(dispatchCarEntityOld.getVersion())) {
                throw exception("保存调度车辆版本号不符");
            }
            //保存调度车辆
            RecDispatchCarEntity recDispatchCarEntity = getEntity(userItem, RecDispatchCarEntity.class);
            String funeralParlourCode = recDispatchCarEntity.getFuneralParlourCode();
            // 覆盖数据
            BeanUtil.copy2Bean(dispatchCar, recDispatchCarEntity);
            recDispatchCarEntity.setFuneralParlourCode(funeralParlourCode);
            //更新调度用户编号及姓名
            if (DataUtil.invalid(recDispatchCarEntity.getDispatchUserId())) {
                recDispatchCarEntity.setDispatchUserId(userItem.getId());
            }
            if (DataUtil.invalid(recDispatchCarEntity.getDispatchUserName())) {
                recDispatchCarEntity.setDispatchUserName(userItem.getFullName());
            }
            //保存接运信息
            recDispatchCarEntity.setRecId(entity.getId());
            //更新人员id
            recDispatchCarEntity.setModifiedUserId(userItem.getId());
            //保存调度信息
            recDispatchCarMapper.updateRecDispatchCar(recDispatchCarEntity);
        }
    }

    /**
     * 更新接运信息抬尸工信息
     *
     * @param userItem 当前帐号
     * @param data     接运数据
     * @param entity   接运任务
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-17 11:05
     */
    private void updateRecTaskPorter(UserItem userItem, RecInfoData data, RecInfoEntity entity) throws
            ActionException {

        // 处理抬尸工
        if (DataUtil.invalid(data.getRecInfoPorter())) {
            // 清除抬尸工
            recInfoDao.deletedRecTaskPorter(entity.getId(), userItem.getId());
        } else {
            // 去重
            data.setRecInfoPorter(DataUtil.repeatArrayList(data.getRecInfoPorter()));

            // 处理抬尸工
            ArrayList<Integer> userIds = recInfoDao.listRecPorterByRecId(data.getId());
            for (Integer userId : data.getRecInfoPorter()) {
                if (DataUtil.invalid(userId)) {
                    continue;
                }
                if (userIds.contains(userId)) {
                    userIds.remove(userId);
                } else {
                    RecPorterEntity porterEntity = getEntity(userItem, RecPorterEntity.class);
                    porterEntity.setRecId(entity.getId())
                            .setSystemUserId(userId);
                    recPorterMapper.insertRecPorter(porterEntity);
                    if (DataUtil.isNull(porterEntity.getId())) {
                        throw exception("保存接运任务抬尸工信息失败");
                    }
                }
            }

            // 去除多余的抬尸工
            if (userIds.size() > 0) {
                // 清除抬尸工
                recInfoDao.deletedRecInfoPorterByUserIds(entity.getId(), userItem.getId(), userIds);
            }
        }
    }

    /**
     * 更新接运信息司机信息
     *
     * @param userItem 当前帐号
     * @param data     接运数据
     * @param entity   接运任务
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-17 11:05
     */
    private void updateRecInfoDriver(UserItem userItem, RecInfoData data, RecInfoEntity entity) throws
            ActionException {

        // 处理抬尸工
        if (DataUtil.invalid(data.getRecInfoPorter())) {
            // 清除抬尸工
            recInfoDao.deletedRecDriver(entity.getId(), userItem.getId());
        } else {
            // 去重
            data.setRecInfoDriver(DataUtil.repeatArrayList(data.getRecInfoDriver()));

            // 获得司机
            ArrayList<Integer> userIds = recInfoDao.listRecDriverByRecId(data.getId());
            for (Integer userId : data.getRecInfoDriver()) {
                if (DataUtil.invalid(userId)) {
                    continue;
                }
                if (userIds.contains(userId)) {
                    userIds.remove(userId);
                } else {
                    RecDriverEntity recDriverEntity = getEntity(userItem, RecDriverEntity.class);
                    recDriverEntity.setRecId(entity.getId())
                            .setSystemUserId(userId);
                    recDriverMapper.insertRecDriver(recDriverEntity);
                    if (DataUtil.isNull(recDriverEntity.getId())) {
                        throw exception("保存接运信息司机失败");
                    }
                }
            }

            // 去除多余的司机
            if (userIds.size() > 0) {
                // 清除司机
                recInfoDao.deletedRecInfoDriverByUserIds(entity.getId(), userItem.getId(), userIds);
            }
        }
    }

    /**
     * @param
     * @return
     * @author ZhangLiRui
     * @date 2020-6-11 11:42
     */
    @Transactional(rollbackFor = Exception.class)
    public BaseResult deleteRecInfowithId(UserItem userItem, IdVersionData data) {
        // 判断删除主键集合是否存在
        if (data == null || DataUtil.invalid(data.getId())) {
            throw exception("不存在要删除信息的主键");
        }

        if (DataUtil.isNull(data.getVersion())) {
            throw exception("版本号不能为空");
        }

        RecInfoEntity entity = recInfoMapper.getRecInfoById(data.getId());

        if (entity == null || DataUtil.invalid(entity.getId())) {
            throw exception("主键错误,不存在此信息");
        }

        if (!DataUtil.equals(data.getVersion(), entity.getVersion())) {
            throw exception(versionError("电话预约接运信息"));
        }

        recInfoMapper.deletedForRecInfo(data.getId(), userItem.getId(), data.getVersion());

        return success("删除完成");
    }

    /**
     * 根据rec_info表中id加载接运信息
     *
     * @param data
     * @return
     * @author ZhangLiRui
     * @date 2020/6/12 9:57
     */
    public BaseResult loadRecInfoWithId(IdData data) {

        if (DataUtil.isNull(data) == null || DataUtil.invalid(data.getId())) {
            return failure("业务主键为空");
        }
        //响应实体
        RecInfoAllLoadData resultData = new RecInfoAllLoadData();

        //根据rec_info表中id加载接运信息
        RecInfoEntity recInfoEntity = recInfoMapper.getRecInfoById(data.getId());
        if (DataUtil.isNull(recInfoEntity)) {
            return failure("业务主键不存在");
        }
        //接运信息响应实体
        BeanUtil.copy2Bean(recInfoEntity, resultData);
        //用户
        ArrayListExt<String> userList = new ArrayListExt<>();
        userList.addExt("transport_task_driver_user");
        userList.addExt("transport_task_porter");

        // 加载用户数据  用户-抬尸工  用户-司机
        resultData.setUsers(userService.loadRoleUserMap(userList));
        //物品相关
        RecInfoAllServiceItemsData serviceItems = new RecInfoAllServiceItemsData();
        // 物品相关-服务物品
        String businessType = EBusinessType.USE_IN_REC_INFO.getType();
        ArrayList<ServiceItem> serviceItemsLoad = serviceItemDao.listServiceItemByBusinessType(businessType);
        HashMap<String, ArrayList<ServiceItem>> dataMap = new HashMap<>(16);
        dataMap.put(businessType, serviceItemsLoad);
        serviceItems.setUseInRecInfo(serviceItemsLoad);
        //物品相关-随车物品
        ArrayList<RecSelItem> recSelItem = recInfoDao.listRecSelItemByRecId(data.getId());
        resultData.setRecSelItem(recSelItem);
        //字典相关
        ArrayListExt<String> dictList = new ArrayListExt<>();
        dictList.addExt(EDictCode.BODY_DEPOSITED_TYPE.getCode());
        dictList.addExt(EDictCode.RELATIONSHIP_WITH_DECEASED.getCode());
        dictList.addExt(EDictCode.REC_RELATION_TYPE.getCode());
        dictList.addExt(EDictCode.REC_SOURCE_TYPE.getCode());
        dictList.addExt(EDictCode.REC_TASK_STATE_TYPE.getCode());
        resultData.setDicts(dictionaryService.loadDictionaryMap(dictList));
        //物品相关-实际物品
        ArrayList<RecActualItem> recActualItem = recInfoDao.listRecActualItemByRecId(data.getId());
        resultData.setRecActualItem(recActualItem);
        //加入物品服务
        resultData.setServiceItems(serviceItems);
        //车辆调度信息
        DispatchCar dispatchCar = recInfoDao.getDispatchCarByRecId(data.getId());
        resultData.setDispatchCar(dispatchCar);
        //接运司机
        ArrayList<Integer> recInfoDriver = recInfoDao.listRecDriverByRecId(data.getId());
        resultData.setRecInfoDriver(recInfoDriver);
        //抬尸工
        ArrayList<Integer> recInfoPorter = recInfoDao.listRecPorterByRecId(data.getId());
        resultData.setRecInfoPorter(recInfoPorter);
        // 加载服务规则
        String dictCode = EDictCode.TRANSPORT_CAR_TYPE.getCode();
        resultData.setRuleItems(serviceItemDao.listServiceDictionaryRuleByDictCode(dictCode, ""));

        // 返回响应结果
        return successData(resultData);
    }

    /**
     * 根据rec_info表中id加载接运打印二维码数据
     *
     * @param data
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-16 10:44
     */

    public BaseResult loadRecInfoPrintCodeWithId(IdData data) {
        if (DataUtil.isNull(data) == null || DataUtil.invalid(data.getId())) {
            return failure("业务主键为空");
        }
        //响应实体
        RecInfoPrintCode resultData = new RecInfoPrintCode();
        //根据rec_info表中id加载接运信息
        RecInfoEntity recInfoEntity = recInfoMapper.getRecInfoById(data.getId());
        if (DataUtil.isNull(recInfoEntity)) {
            return failure("业务主键不存在");
        }
        //接运信息响应实体
        BeanUtil.copy2Bean(recInfoEntity, resultData);
        return successData(resultData);
    }

    /**
     * 根据rec_info表中id加载接运派车单数据
     *
     * @param data
     * @return
     * @author GaoXiangLiang
     * @date 2020-06-16 10:44
     */
    public BaseResult loadRecInfoPrintTruckingOrderWithId(IdData data) {

        if (DataUtil.isNull(data) == null || DataUtil.invalid(data.getId())) {
            return failure("业务主键为空");
        }
        //响应实体
        RecInfoTrackOrder recInfoTrackOrder = new RecInfoTrackOrder();
        //根据rec_info表中id加载接运信息
        RecInfoEntity recInfoEntity = recInfoMapper.getRecInfoById(data.getId());
        DispatchCarItem dispatchCarItem = recDispatchCarDao.selectRecDispatchCarByRecId(data.getId());
        //判断非空
        if (!DataUtil.isNull(dispatchCarItem) && !DataUtil.isNull(dispatchCarItem.getRecCarNo())) {
            recInfoTrackOrder.setRecCarNo(dispatchCarItem.getRecCarNo());
        }
        if (DataUtil.isNull(recInfoEntity)) {
            return failure("业务主键不存在");
        }
        //接运信息响应实体
        BeanUtil.copy2Bean(recInfoEntity, recInfoTrackOrder);
        //接运司机
        String recDrivers = recInfoDao.getDriverNameByRecId(data.getId());
        recInfoTrackOrder.setRecDrivers(recDrivers);

        //抬尸工拼接
        String recPorters = recInfoDao.getPorterNameByRecId(data.getId());
        //载入抬尸工
        recInfoTrackOrder.setRecPorters(recPorters);
        //随车物品明细
        ArrayList<RecSelItem> recSelItems = recInfoDao.listRecSelItemByRecId(data.getId());
        //随车物品明细添加到返回值中
        recInfoTrackOrder.setRecSelItems(recSelItems);

        return successData(recInfoTrackOrder);
    }

    /**
     * 根据rec_info表中id加载接运信息
     *
     * @param data
     * @return
     * @author GaoXiangLiang
     * @date 2020-6-12 9:57
     */
    public BaseResult loadRecInfoAll(RecInfoAllRequestData data) {
        //根据rec_info表中id加载接运信息
        RecInfoEntity recInfoEntity = recInfoMapper.getRecInfoById(data.getId());
        if (DataUtil.isNull(data.getId()) && (DataUtil.isNull(recInfoEntity) || DataUtil.isNull(recInfoEntity.getId(), recInfoEntity.getVersion()))) {
            return failure(MessageConst.NO_ID);
        }

        //接运信息响应实体
        RecInfoAllLoadData recInfoAllLoadData = new RecInfoAllLoadData();
        BeanUtil.copy2Bean(recInfoEntity, recInfoAllLoadData);

        // 加载用户数据  用户-抬尸工  用户-司机
        if (DataUtil.valid(data.getUserRoleCodes())) {
            recInfoAllLoadData.setUsers(userService.loadRoleUserMap(data.getUserRoleCodes()));
        }
        //物品相关
        RecInfoAllServiceItemsData serviceItems = new RecInfoAllServiceItemsData();
        // 物品相关-服务物品
        if (DataUtil.valid(data.getServiceItemTypeCode())) {
            String businessType = EBusinessType.USE_IN_REC_INFO.getType();
            ArrayList<ServiceItem> serviceItemsLoad = serviceItemDao.listServiceItemByBusinessType(businessType);
            HashMap<String, ArrayList<ServiceItem>> dataMap = new HashMap<>(16);
            dataMap.put(businessType, serviceItemsLoad);
            serviceItems.setUseInRecInfo(serviceItemsLoad);
        }
        //物品相关-随车物品
        ArrayList<RecSelItem> recSelItem = recInfoDao.listRecSelItemByRecId(data.getId());
        recInfoAllLoadData.setRecSelItem(recSelItem);
        //字典相关
        // 加载字典数据
        recInfoAllLoadData.setDicts(dictionaryService.loadDictionaryMap(data.getDictTypeCodes()));
        //物品相关-实际物品
        ArrayList<RecActualItem> recActualItem = recInfoDao.listRecActualItemByRecId(data.getId());
        recInfoAllLoadData.setRecActualItem(recActualItem);
        //加入物品服务
        recInfoAllLoadData.setServiceItems(serviceItems);
        //调度信息
        DispatchCar dispatchCar = recInfoDao.getDispatchCarByRecId(data.getId());
        recInfoAllLoadData.setDispatchCar(dispatchCar);
        //接运司机
        ArrayList<Integer> recInfoDriver = recInfoDao.listRecDriverByRecId(data.getId());
        recInfoAllLoadData.setRecInfoDriver(recInfoDriver);
        //抬尸工
        ArrayList<Integer> recInfoPorter = recInfoDao.listRecPorterByRecId(data.getId());
        recInfoAllLoadData.setRecInfoPorter(recInfoPorter);
        // 加载服务规则
        String dictCode = EDictCode.TRANSPORT_CAR_TYPE.getCode();
        recInfoAllLoadData.setRuleItems(serviceItemDao.listServiceDictionaryRuleByDictCode(dictCode, ""));
        //返回调度必填标记
        if (configApp.getRecDispatchEnable()) {
            recInfoAllLoadData.setRecDispatchEnable(configApp.getRecDispatchEnable());
        } else {
            recInfoAllLoadData.setRecDispatchEnable(configApp.getRecDispatchEnable());
        }

        // 返回响应结果
        return successData(recInfoAllLoadData);
    }

    /**
     * 根据rec_info表中id加载微信端接运信息
     *
     * @param data
     * @return
     * @author GaoXiangLiang
     * @date 2020-6-12 9:57
     */
    public BaseResult loadRecInfoWXAll(RecInfoAllRequestData data) {
        return loadRecInfoAll(data);
    }

    /**
     * 获得指定日期和appId号的接运信息列表
     *
     * @param data
     * @return
     * @author GaoXiangLiang
     * @date 2020-6-12 9:57
     */
    public BaseResult loadRecInfoWXAllListByAppId(RecInfoSearh data) {
        //根据rec_info表中id加载接运信息
        ArrayList<RecInfoAllWxData> recList = recInfoDao.listRecInfo(data);
        ArrayList<RecInfoAllLoadData> recInfoAllLoadDataList = new ArrayList<RecInfoAllLoadData>();
        if (!recList.isEmpty()) {
            RecInfoAllWxData recInfoWx = null;
            RecInfoEntity recInfoEntity = null;
            for (int i = 0; i < recList.size(); i++) {
                recInfoWx = recList.get(i);
                recInfoEntity = new RecInfoEntity();
                BeanUtil.copy2Bean(recInfoWx, recInfoEntity);
                if (!DataUtil.isNull(recInfoEntity)) {
                    RecInfoAllLoadData recInfoAllLoadData = getRecInfoAllLoadDataFromRecInfo(recInfoEntity);
                    recInfoAllLoadDataList.add(recInfoAllLoadData);
                }

            }
        }
        RecInfoWxData listWxObject = new RecInfoWxData();
        listWxObject.setList(recInfoAllLoadDataList);
        // 返回响应结果
        return successData(listWxObject);
    }

    /**
     * 获得指定日期的未完成接运信息列表
     *
     * @param data
     * @return
     * @author GaoXiangLiang
     * @date 2020-6-12 9:57
     */
    public BaseResult loadRecInfoAppNotFinishList(RecInfoSearh data) {
        //根据rec_info表中id加载接运信息
        ArrayList<RecInfoWx> recList = recInfoDao.listUnFinishRecInfo(data);
        //实例化返回数据
        ArrayList<RecInfoAllLoadData> recInfoAllLoadDataList = new ArrayList<RecInfoAllLoadData>();
        if (!recList.isEmpty()) {
            RecInfoWx recInfoWx = null;
            RecInfoEntity recInfoEntity = null;
            for (int i = 0; i < recList.size(); i++) {
                recInfoWx = recList.get(i);
                recInfoEntity = new RecInfoEntity();
                BeanUtil.copy2Bean(recInfoWx, recInfoEntity);
                if (!DataUtil.isNull(recInfoEntity)) {
                    RecInfoAllLoadData recInfoAllLoadData = getRecInfoAllLoadDataFromRecInfo(recInfoEntity);
                    recInfoAllLoadDataList.add(recInfoAllLoadData);
                }

            }
        }
        RecInfoWxData listWxObject = new RecInfoWxData();
        listWxObject.setList(recInfoAllLoadDataList);
        // 返回响应结果
        return successData(listWxObject);
    }

    /**
     * 通过接运二维码或接运id查询用户接运信息车辆的轨迹点
     *
     * @param data
     * @return
     * @author GaoXiangLiang
     * @date 2020-6-12 9:57
     */
    public BaseResult loadRecInfoWXTrackByRecCode(RecInfoSearh data) {
        //根据rec_info表中查询车辆牌号
        if (!DataUtil.isNull(data) && (DataUtil.invalid(data.getRecCode()) || DataUtil.invalid(data.getId()))) {
            Integer recId = recInfoDao.getRecIdByRecCode(data.getRecCode());
            recId = recId == null ? data.getId() : recId;
            ArrayList<RecGpsInfo> trackList = recInfoDao.listTrackListByRecId(recId);
            String carNo = recInfoDao.getCarNoByRecId(recId);
            String drivers = recInfoDao.getDriverNameByRecId(recId);
            RecGpsInfoData recGpsInfoData = new RecGpsInfoData();
            //车牌号
            recGpsInfoData.setCarNo(carNo);
            //司机多名
            recGpsInfoData.setDrivers(drivers);
            //轨迹点
            recGpsInfoData.setList(trackList);
            //返回结果
            // 返回响应结果
            return successData(recGpsInfoData);
        } else {
            //没有结果
            return successData(new RecGpsInfoData());
        }

    }

    /**
     * 添加随车物品的接口
     *
     * @param data
     * @return
     * @author GaoXiangLiang
     * @date 2020-6-12 9:57
     */
    public BaseResult loadRecInfoWXDialogServiceItems(RecInfoAllRequestData data) {
        //物品集合
        ArrayList<ServiceItem> serviceItemsLoad = new ArrayList<ServiceItem>();
        // 物品相关-服务物品
        if (DataUtil.valid(data.getServiceItemTypeCode())) {
            //查询接运物品
            String businessType = EBusinessType.USE_IN_REC_INFO.getType();
            serviceItemsLoad = serviceItemDao.listServiceItemByBusinessType(businessType);
        }
        RecInfoAllLoadData resultData = new RecInfoAllLoadData();
        //服务物品返回对象
        RecInfoAllServiceItemsData serviceItemsResult = new RecInfoAllServiceItemsData();
        //查询出接运对应的物品项
        serviceItemsResult.setUseInRecInfo(serviceItemsLoad);
        //加入返回值中
        resultData.setServiceItems(serviceItemsResult);
        // 返回响应结果
        return successData(resultData);
    }

    /**
     * 装箱接运信息
     */
    private RecInfoAllLoadData getRecInfoAllLoadDataFromRecInfo(RecInfoEntity recInfoEntity) {
        if (DataUtil.isNull(recInfoEntity)) {
            return null;
        }
        //接运信息响应实体
        RecInfoAllLoadData recInfoAllLoadData = new RecInfoAllLoadData();
        BeanUtil.copy2Bean(recInfoEntity, recInfoAllLoadData);
        //物品相关
        RecInfoAllServiceItemsData serviceItems = new RecInfoAllServiceItemsData();
        //物品相关-随车物品
        ArrayList<RecSelItem> recSelItem = recInfoDao.listRecSelItemByRecId(recInfoEntity.getId());
        recInfoAllLoadData.setRecSelItem(recSelItem);
        //物品相关-实际物品
        ArrayList<RecActualItem> recActualItem = recInfoDao.listRecActualItemByRecId(recInfoEntity.getId());
        recInfoAllLoadData.setRecActualItem(recActualItem);
        //加入物品服务
        recInfoAllLoadData.setServiceItems(serviceItems);
        //调度信息
        DispatchCar dispatchCar = recInfoDao.getDispatchCarByRecId(recInfoEntity.getId());
        recInfoAllLoadData.setDispatchCar(dispatchCar);
        //接运司机
        ArrayList<Integer> recInfoDriver = recInfoDao.listRecDriverByRecId(recInfoEntity.getId());
        recInfoAllLoadData.setRecInfoDriver(recInfoDriver);
        //抬尸工
        ArrayList<Integer> recInfoPorter = recInfoDao.listRecPorterByRecId(recInfoEntity.getId());
        recInfoAllLoadData.setRecInfoPorter(recInfoPorter);
        // 加载服务规则
        String dictCode = EDictCode.TRANSPORT_CAR_TYPE.getCode();
        recInfoAllLoadData.setRuleItems(serviceItemDao.listServiceDictionaryRuleByDictCode(dictCode, ""));
        return recInfoAllLoadData;
    }

    /**
     * 装箱接运信息到微信信息
     */
    private RecInfoAllWxData getRecInfoAllWxDataFromRecInfo(RecInfoEntity recInfoEntity) {
        if (DataUtil.isNull(recInfoEntity)) {
            return null;
        }
        //接运信息响应实体
        RecInfoAllWxData recInfoAllWxData = new RecInfoAllWxData();
        BeanUtil.copy2Bean(recInfoEntity, recInfoAllWxData);
        //物品相关
        RecInfoAllServiceItemsData serviceItems = new RecInfoAllServiceItemsData();
        //物品相关-随车物品
        ArrayList<RecSelItem> recSelItem = recInfoDao.listRecSelItemByRecId(recInfoEntity.getId());
        recInfoAllWxData.setRecSelItem(recSelItem);
        //物品相关-实际物品
        ArrayList<RecActualItem> recActualItem = recInfoDao.listRecActualItemByRecId(recInfoEntity.getId());
        recInfoAllWxData.setRecActualItem(recActualItem);
        //加入物品服务
        recInfoAllWxData.setUseInRecInfo(new ArrayList<ServiceItem>());
        //调度信息
        DispatchCar dispatchCar = recInfoDao.getDispatchCarByRecId(recInfoEntity.getId());
        recInfoAllWxData.setDispatchCar(dispatchCar);
        //接运司机
        ArrayList<Integer> recInfoDriver = recInfoDao.listRecDriverByRecId(recInfoEntity.getId());
        recInfoAllWxData.setRecInfoDriver(recInfoDriver);
        //抬尸工
        ArrayList<Integer> recInfoPorter = recInfoDao.listRecPorterByRecId(recInfoEntity.getId());
        recInfoAllWxData.setRecInfoPorter(recInfoPorter);
        // 加载服务规则
        String dictCode = EDictCode.TRANSPORT_CAR_TYPE.getCode();
        recInfoAllWxData.setRuleItems(serviceItemDao.listServiceDictionaryRuleByDictCode(dictCode, ""));
        return recInfoAllWxData;
    }

    /**
     * 获取新的业务编码
     *
     * @param
     * @return
     * @author LiCongLu
     * @date 2020-02-06 9:48
     */
    public String getRecInfoRecCodeAsNew() {
        Date nowDate = nowDate();

        // 前缀编码
        String prefixCode = configData.getRecBusinessCodePrefix() + DateUtil.formatPattern(nowDate, configData.getRecBusinessCodeDatePattern());
        // 自增码长度
        int autoLength = configData.getRecBusinessCodeAutoLength();

        // 自增起始码
        int autoNumber = 0;

        // 最后的业务编码
        String lastCode = recInfoDao.getRecCodeAsLast();

        // 存在最后的业务编码，且是当天的业务编码
        if (DataUtil.valid(lastCode) && lastCode.startsWith(prefixCode)) {
            // 得到最后的自增编码
            lastCode = lastCode.replace(prefixCode, "");
            int lastNumber = Integer.parseInt(lastCode);
            autoNumber = lastNumber;
        }

        // 拼接最新业务编码
        String newCode = prefixCode + String.format("%0" + String.valueOf(autoLength) + "d", ++autoNumber);
        return newCode;
    }

    /**
     * 更新预约状态车辆为确认出车状态
     *
     * @param
     * @return
     * @author GaoXiangLiang
     * @date 2020/6/22 17:47
     */
    @Transactional(rollbackFor = Exception.class)
    public BaseResult confirmRecInfoAppOutCar(UserItem userItem, RecInfoData data) throws ActionException {
        //主键id
        Integer id = data.getId();

        // 业务编码不为空
        if (DataUtil.invalid(id)) {
            // 不存在接运编码时，认定为新增，生成新业务编码
            throw exception("确认出车失败");

        } else {
            confirmOutCar(userItem, data);
        }

        return loadRecInfoWithId(new IdData(id));
    }

    /**
     * 更新预约状态车辆为已完成或返空车状态
     *
     * @param
     * @return
     * @author GaoXiangLiang
     * @date 2020/6/24 17:47
     */
    @Transactional(rollbackFor = Exception.class)
    public BaseResult confirmRecInfoAppBackCar(UserItem userItem, RecInfoData data) throws ActionException {
        //主键id
        Integer id = data.getId();

        // 业务编码不为空
        if (DataUtil.invalid(id)) {
            // 不存在接运编码时，认定为新增，生成新业务编码
            throw exception("确认出车失败");

        } else {
            //确认回车
            confirmBackCar(userItem, data);
        }

        return loadRecInfoWithId(new IdData(id));
    }

    /**
     * 更新接运信息状态【已出车】->【已完成】
     *
     * @param userItem
     * @param data
     * @throws ActionException
     */
    private void confirmBackCar(UserItem userItem, RecInfoData data) throws ActionException {
        // 验证主键和数据版本是否存在
        RecInfoEntity recInfoEntity = validataRecInfo(userItem, data);
        if (!DataUtil.isNull(recInfoEntity)) {
            //已调度车辆，接运信息为已出车
            if (recInfoEntity.getTaskStateCode().intValue() == taskStateCode.getYiChuChe().intValue()) {
                //更新车辆接运信息状态为已出车状态
                recInfoEntity.setTaskStateCode(taskStateCode.getYiHuiChe());
                recInfoEntity.setTaskState(taskStateCode.getYiHuiChe_name());
                data.setTaskStateCode(taskStateCode.getYiHuiChe());
                data.setTaskState(taskStateCode.getYiHuiChe_name());
                //更新确认出车的状态
                updateRecInfo(userItem, data, recInfoEntity);
            } else {
                throw exception("请求非调度车辆不能确认出车");
            }
        }

    }

    /**
     * CS端加载车辆调度信息列表，带分页
     *
     * @param data
     * @return
     * @author GaoXiangLiang
     * @date 2020-6-12 9:57
     */
    public BaseResult loadRecInfoCsAllByPage(RecInfoSearh data) {
        //根据rec_info表中id加载接运信息
        // 初始化分页数据
        startPageData(data);
        ArrayList<RecInfoAllWxData> recList = recInfoDao.listRecInfo(data);
        //查询分页，只查询一次
        SearchLoadData<RecInfoAllWxData> responseData = getSearchLoadData(recList);


        if (!recList.isEmpty()) {
            RecInfoAllWxData recInfoAllWxData = null;
            RecInfoEntity recInfoEntity = null;
            for (int i = 0; i < recList.size(); i++) {
                recInfoAllWxData = recList.get(i);
                recInfoEntity = getEntity(RecInfoEntity.class);
                BeanUtil.copy2Bean(recInfoAllWxData, recInfoEntity);
                if (!DataUtil.isNull(recInfoEntity)) {
                    RecInfoAllWxData recInfoAllLoadData = getRecInfoAllWxDataFromRecInfo(recInfoEntity);
                    recInfoAllWxData = recInfoAllLoadData;
                }

            }
        }
        // 返回响应结果
        return successData(responseData);
    }
}
